<?php
  class headersComponents extends sfComponents
  {

	public function executeNewmp(sfWebRequest $request)
    {		
		$q = Doctrine_Query::create()
		->from('sfGuardUserProfile n')
		->leftJoin("n.Skins")
		->leftJoin("n.Mp_2 u WITH u.lu = 0")
		->where('n.user_id = ?',$this->sf_cache_key);
		$res = $q->fetchOne();
		$this->nbNewMp = count($res["Mp_2"]);
		$this->nbPoint = $res->getNbPoint();
		
		$this->getUser()->setAttribute ("skin_selected", strtolower($res["Skins"]["ident"]));
	}
	public function executeArbo(sfWebRequest $request)
    {
		if($request->getParameter("id"))
			$this->ParamId = $request->getParameter("id");
		
		$this->module = $request->getParameter('module');
		$this->action = $request->getParameter('action');
			
		if(!empty($this->action))
		{
			switch($this->action)
			{
				
				case "new": case "create": case "newdef":
					$this->nameAction = "Création ";
				break;
				case "edit": case "update":
					$this->nameAction = "Edition ";
				break;
				case "options":
					$this->nameAction = "Sélection ";
				break;
				case "show":
					$this->nameAction = "Affichage ";
				break;
				case "version":
					$this->nameAction = "Création d'une version";
				break;
				case "lexique":case "commun":case "platform":case "article":case "favoris":
					$this->nameAction = "Résultats";
				break;
				case "viewrevision":case "compare":
					$this->nameAction = "Révisions ";
				break;
				/*case "new":
					$this->nameAction = "Créati²on ";
				break;*/
			}
		}
		
		$this->nameModule = $request->getParameter("NameModule");
		$this->nameType = $request->getParameter("NameType");
	}
	public function executeModeration(sfWebRequest $request)
    {
		switch($this->Mtype)
		{
			case "add":
				$this->img = "add.png";
				$this->title = "Ajouter un nouvel élément";
			break;
			case "edit":
				$this->img = "edit.png";
				$this->title = "Editer l'élément";
			break;
			case "show":
				$this->img = "book.png";
				$this->title = "Afficher l'élément";
			break;
			case "delete":
				$this->img = "delete.png";
				$this->title = "Supprimer l'élément";
			break;
			case "validate":
				$this->img = "yes.png";
				$this->title = "Valider l'élément";
			break;
			case "punir":
				$this->img = "user.png";
				$this->title = "Avertir ou punir l'auteur";
			break;
			case "alert":
				$this->img = "error.png";
				$this->title = "Signaler un abus";
			break;
			case "online":
				$this->img = "power_off.png";
				$this->title = "Mettre en/hors ligne";
			break;
			
		}	
			$this->Right = $this->MCred;
			
			$this->Url = $this->MModule;
			$this->Url .= "/".$this->MAction;
			
			if(isset($this->Mid))
				$this->Url .= "?id=".$this->Mid;
			
			if(isset($this->Mid2))
				$this->Url .= "&id2=".$this->Mid2;
	}
	public function executePager(sfWebRequest $request)
    {
		if(!isset($this->params))
			$this->params = '';
		elseif(!empty($this->params))
			$this->params .= '&';
		
		if(!isset($this->optNbTotal))
			$this->optNbTotal = true;
		if(!isset($this->optImgNextPrev))
			$this->optImgNextPrev = true;
		if(!isset($this->optNumPage))
			$this->optNumPage = true;
	}
	public function executeAdvice(sfWebRequest $request)
    {
		$filecache = "./cache/list_advices.cache";
		
		if(!is_file($filecache))
		{
			$q = Doctrine_Query::create()
			->from('advices n');
			$advices = $q->execute();
			$ad_final = array();
			foreach($advices as $ad)
			{
				$ad_final[] = $ad["advice"];
				
			}
			@file_put_contents($filecache, serialize($advices));
			$this->advice = $ad_final[0]["advice"];
		}
		else 
		{
			$advices = unserialize(file_get_contents($filecache));
			$this->advice = $advices[rand(0, count($advices) - 1)]["advice"];
		}
	}
}
